package thirdwork.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {

	private static String driver = "com.mysql.cj.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/staff?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true";
	private static String username = "liushichen";
	private static String password = "123456";

	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;

	/**
	 * 获取链接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		} finally {

		}
		return conn;
	}

	/**
	 * 关闭所有流
	 */
	public static void closeAll() {
		// 关闭结果集
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}

		// 关闭预处理
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
		// 关闭连接
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
